package com.dongdongshop.controller;

import com.alipay.api.AlipayApiException;
import com.dongdongshop.data.Result;
import com.dongdongshop.service.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;



@Controller
@RequestMapping("alipay")
@CrossOrigin
public class AliPayController {

    @Autowired
    private AliPayService aliPayService;


    /**
     * @param out_trade_no 商户订单号
     * @param total_amount 总金额
     * @param subject 商品描述
     * @return
     * @throws AlipayApiException
     */
    @PostMapping("tradePay")  //支付
    @ResponseBody
    public String tradePay(@RequestParam String out_trade_no,@RequestParam String total_amount,@RequestParam String subject) {
        String s = aliPayService.tradePay(out_trade_no, total_amount, subject);
        return s;
    }

    /**
     *
     * @param out_trade_no 商户订单号
     * @param trade_no  支付宝流水号
     * @return 交易查询
     */
    @PostMapping("queryTrade")
    @ResponseBody
    public Result queryTrade(@RequestParam String out_trade_no,@RequestParam String trade_no){
        Result result = aliPayService.queryTrade(out_trade_no, trade_no);
        return result;
    }


    /**
     * @param trade_no 流水号
     * @param refund_amount 退款金额
     * @param out_request_no 退款请求号
     * @return
     */
    @PostMapping("tradeRefund")
    @ResponseBody
    public Result tradeRefund(@RequestParam String trade_no,@RequestParam String refund_amount,@RequestParam String out_request_no,@RequestParam Long id){
        Result result = aliPayService.tradeRefund(trade_no, refund_amount, out_request_no, id);
        return result;

    }


}
